package com.jwds.digital.bios.business.schedule.task;

import com.jwds.digital.bios.business.service.service.belt.BeltCoreHandleLineService;
import com.jwds.digital.bios.business.service.service.belt.IBeltCoreHandleService;
import com.jwds.digital.bios.business.service.service.belt.IBeltProtectionStatusService;
import com.jwds.digital.bios.business.service.service.belt.IBeltRouteJobCoreHandleService;
import com.jwds.tdc.framework.schedule.annotation.ScheduleRegister;
import com.xxl.job.core.handler.annotation.XxlJob;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * <p>BeltCloudSyncTask 皮带云相关定时任务</p>
 *
 * @author 林柏云
 * @Date：2024/12/13 10:13
 */
@Component
@Slf4j
public class BeltCloudSyncTask {


  @Resource
  private IBeltCoreHandleService beltCoreHandleService;

  @Resource
  private IBeltProtectionStatusService beltProtectionStatusService;

  @Resource
  private IBeltRouteJobCoreHandleService beltRouteJobCoreHandleService;

  /**
   * 更新正在执行的路线 每1分钟一次
   */
  @XxlJob("defend-running-routes")
  @ScheduleRegister(cron = "0 0/1 * * * ? ", author = "linboyun", jobDesc = "更新正在执行的路线")
  public void defendRunningRoutes(){
    log.warn("==更新正在执行的路线==");
    beltCoreHandleService.defendRunningRoutes();
  }

  /**
   * 分钟级别表参数收集 每1分钟一次
   */
  @XxlJob("belt-minute-auto-collect")
  @ScheduleRegister(cron = "0 0/1 * * * ? ", author = "linboyun", jobDesc = "皮带分钟级别数据收集")
  public void beltMinuteAutoCollect() {
    log.warn("==皮带分钟级别数据收集==");
    beltCoreHandleService.doBeltMinuteAutoCollect();
  }

  /**
   * 小时级别表参数收集 每10分钟一次
   */
  @XxlJob("belt-hour-auto-collect")
  @ScheduleRegister(cron = "0 0/10 * * * ? ", author = "linboyun", jobDesc = "皮带小时级别数据收集")
  public void beltHourAutoCollect() {
    log.warn("==皮带小时级别数据收集==");
    beltCoreHandleService.doBeltHourAutoCollect();
  }

  /**
   * 天级别表参数收集 每10分钟一次
   */
  @XxlJob("belt-day-auto-collect")
  @ScheduleRegister(cron = "0 0/10 * * * ? ", author = "linboyun", jobDesc = "皮带天级别数据收集")
  public void beltDayAutoCollect() {
    log.warn("==皮带天级别数据收集==");
    beltCoreHandleService.doBeltDayAutoCollect();
  }

  /**
   * 周级别表参数收集 每10分钟一次
   */
  @XxlJob("belt-week-auto-collect")
  @ScheduleRegister(cron = "0 0/10 * * * ? ", author = "linboyun", jobDesc = "皮带周级别数据收集")
  public void beltWeekAutoCollect() {
    //log.warn("==皮带周级别数据收集==");
    beltCoreHandleService.doBeltWeekAutoCollect();
  }

  /**
   * 月级别表参数收集 每10分钟一次
   */
  @XxlJob("belt-month-auto-collect")
  @ScheduleRegister(cron = "0 0/10 * * * ? ", author = "linboyun", jobDesc = "皮带月级别数据收集")
  public void beltMonthAutoCollect() {
    //log.warn("==皮带月级别数据收集==");
    beltCoreHandleService.doBeltMonthAutoCollect();
  }

  /**
   * 更新天历史指标 每天一次
   */
  @XxlJob("update_belt_day_target_his")
  @ScheduleRegister(cron = "0 0/30 * * * ? ", author = "linboyun", jobDesc = "更新天历史指标")
  public void updateBeltDayTargetHis() {
    log.warn("==更新天历史指标==");
    beltRouteJobCoreHandleService.updateBeltDayTargetHis();
  }
  @Resource
  private BeltCoreHandleLineService beltCoreHandleLineService;

  /**
   * 小时级别表参数收集 每一小时一次
   */
  @XxlJob("calculateOneHourBeltPerformance")
  @ScheduleRegister(cron = "0 0 * * * ?", author = "mochen", jobDesc = "每小时计算皮带效能数据")
  public void calculateOneHourBeltPerformance() {
    // 计算当前时间前一小时的时间范围
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime startTime = now.minusHours(1).withMinute(0).withSecond(0).withNano(0);
    LocalDateTime endTime = now.withMinute(0).withSecond(0).withNano(0);

    // 调用 doOneHourCalculateByOneMinData 方法
    beltCoreHandleLineService.doOneHourCalculateByOneMinData(
            startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
            endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
    );
  }

  /**
   * 天级别表参数收集 每一天一次
   */
  @XxlJob("doOneHourLineCalculateByOneDayDataJob")
  @ScheduleRegister(cron = "0 0 0 * * ?", author = "mochen", jobDesc = "每天计算前一天的小时级数据")
  public void doOneHourLineCalculateByOneDayDataJob() {
    // 获取前一天的日期
    LocalDate yesterday = LocalDate.now().minusDays(1);


    // 设置开始时间和结束时间（前一天的 00:00:00 到 23:59:59）
    LocalDateTime startTime = LocalDateTime.of(yesterday, LocalTime.of(0, 0, 0)); // 明确指定秒为 0
    LocalDateTime endTime = LocalDateTime.of(yesterday, LocalTime.of(23, 59, 59)); // 明确指定秒为 59
   /* LocalDateTime startTime = LocalDateTime.of(yesterday, java.time.LocalTime.MIDNIGHT);
    LocalDateTime endTime = startTime.plusDays(1).minusSeconds(1);*/

    // 格式化为字符串
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String startTimeStr = startTime.format(formatter);
    String endTimeStr = endTime.format(formatter);

    // 调用业务方法
    beltCoreHandleLineService.doOneHourLineCalculateByOneDayData(startTimeStr, endTimeStr);
  }
  /**
   * 周级别表参数收集 每一周一次
   */
  @XxlJob("calculateOneWeekBeltPerformance")
  @Scheduled(cron = "0 0 0 * * 0", zone = "Asia/Shanghai") // 每周日凌晨0点执行
  public void calculateOneWeekBeltPerformance() {
    // 计算上周的时间范围
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime lastSunday = now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)); // 获取上周日
    LocalDateTime lastMonday = lastSunday.minusDays(6); // 获取上周一
    LocalDateTime startTime = lastMonday.withHour(0).withMinute(0).withSecond(0);
    LocalDateTime endTime = lastSunday.withHour(23).withMinute(59).withSecond(59);

    // 调用 doOneWeekLineCalculateByOneDayData 方法
    beltCoreHandleLineService.doOneWeekLineCalculateByOneDayData(
            startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
            endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
    );
  }

  /**
   * 每月一号凌晨0点执行，计算上月皮带效能数据
   */
  @XxlJob("calculateOneMonthBeltPerformance")
  @Scheduled(cron = "0 0 0 1 * ?", zone = "Asia/Shanghai") // 每月1号凌晨0点执行
  public void calculateOneMonthBeltPerformance() {
    // 计算上月的时间范围
    LocalDate now = LocalDate.now();
    LocalDate firstDayOfLastMonth = now.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
    LocalDate lastDayOfLastMonth = now.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth());

    LocalDateTime startTime = firstDayOfLastMonth.atStartOfDay();
    LocalDateTime endTime = lastDayOfLastMonth.atTime(23, 59, 59);

    // 调用 doOneMonthLineCalculateByOneWeekData 方法
    beltCoreHandleLineService.doOneMonthLineCalculateByOneWeekData(
            startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
            endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
    );
  }

  /**
   * 路线任务统计 每5分钟一次
   */
  @XxlJob("do_statistics_route_job")
  @ScheduleRegister(cron = "0 0/5 * * * ? ", author = "linboyun", jobDesc = "路线任务统计")
  public void doStatisticsRouteJob() {
    log.warn("==路线任务统计==");
    beltRouteJobCoreHandleService.doStatisticsRouteJob();
  }

  /**
   * 实时获取五防信号
   */
  @XxlJob("query_device_status_data_map")
  @ScheduleRegister(cron = "0 0/5 * * * ? ", author = "秦伟烽", jobDesc = "实时获取五防信号")
  private void queryDeviceStatusDataMap() {
    beltProtectionStatusService.queryDeviceStatusDataMap();
  }

  /**
   * 路线异常检查 每10分钟一次
   */
  @XxlJob("do_route_anomaly_check")
  @ScheduleRegister(cron = "0 0/10 * * * ? ", author = "薛振昊", jobDesc = "路线异常检查")
  public void doRouteAnomalyCheck() {
    beltRouteJobCoreHandleService.doRouteAnomalyCheck();
  }

  /**
   * 皮带异常检查 每5分钟一次
   */
  @XxlJob("do_belt_anomaly_check")
  @ScheduleRegister(cron = "0 0/5 * * * ? ", author = "薛振昊", jobDesc = "皮带异常检查")
  public void doBeltAnomalyCheck() {
    beltRouteJobCoreHandleService.doBeltAnomalyCheck();
  }

  /**
   * 五防健康检查 每30分钟一次
   */
  @XxlJob("do_five_defense_check")
  @ScheduleRegister(cron = "0 0/30 * * * ? ", author = "薛振昊", jobDesc = "五防健康检查")
  public void doFiveDefenseCheck() {
    beltRouteJobCoreHandleService.doFiveDefenseCheck();
  }

}
